package com.hd.core.project.module.controller;

import java.util.Arrays;

import java.util.List;
import com.hd.core.project.module.base.WrapperBaseController;
import com.hd.core.project.module.config.StudentModuleProperties;
import com.hd.core.project.module.domain.Select2;
import com.hd.core.project.module.domain.Dormitory;
import com.hd.core.project.module.service.ClassesService;
import com.hd.core.project.module.service.DormitoryService;
import com.hd.core.project.module.service.StudentService;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hd.core.framework.aspectj.lang.annotation.Log;
import com.hd.core.framework.aspectj.lang.constant.BusinessType;
import com.hd.core.framework.web.page.TableDataInfo;
import com.hd.core.framework.web.domain.AjaxResult;

/**
 * <p>宿舍管理 <p/>
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V1.0
 * @author RichardTang 
 * @date: 2018年8月20日
 * @package com.hd.core.project.module.controller
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
@Controller
@SuppressWarnings("all")
@RequestMapping("/module/student/dormitory")
public class DormitoryController extends WrapperBaseController<StudentModuleProperties> {
	
	@Autowired
	private StudentService studentService;
	
	@Autowired
	private ClassesService classesService;
	
	@Autowired
	private DormitoryService dormitoryService;
	
	DormitoryController() {
		super("/dormitory");
	}
	
	/**
	 * <p>跳转到宿舍管理视图</p>
	 * <p>访问权限: module-teacher:class-roster:view</p>
	 * <p>路径: /templates/module/student/dormitory/dormitory.html</p>
	 * 
	 * @return 宿舍管理视图
	 */
	@GetMapping
	@RequiresPermissions("module-student:dormitory:view")
	public String entity(){
	    return getResponseViewPath("/dormitory");
	}
	
	/**
	 * <p>查询宿舍根据条件实体查询<p/>
	 * <p>访问权限: module-student:dormitory:list</p>
	 * 
	 * @param entity 条件实体
	 * @return 符合条件的宿舍数据
	 */
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("module-student:dormitory:list")
	public TableDataInfo list(Dormitory entity){
		startPage();
        List<Dormitory> list = dormitoryService.selectList(entity);
		return getDataTable(list);
	}
	
	/**
	 * <p>跳转到添加视图</p>
	 * <p>路径: /templates/module/student/dormitory/add.html</p>
	 * 
	 * @return 添加功能视图
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap){
	    return getResponseViewPath("/add");
	}
	
	/**
	 * <p>新增保存宿舍信息<p/>
	 * <p>根据Entity实体内的成员属性的值来当做查询条件<p/>
	 * <p>访问权限: module-student:dormitory:add</p>
	 * 
	 * @param entity 条件实体
	 * @param studentId 学生id
	 * @return 符合条件的学生数据
	 */
	@ResponseBody
	@PostMapping("/add")
	@Log(title = "宿舍列表", action = BusinessType.INSERT)
	@RequiresPermissions("module-student:dormitory:add")
	public AjaxResult addSave(Dormitory entity,String[] studentId){
		boolean result = dormitoryService.insert(entity,studentId);
		return	toAjax(result);
	}

	/**
	 * <p>根据id查询出数据并跳转到修改的视图</p>
	 * <p>路径: /templates/module/student/dormitory/edit.html</p>
	 * 
	 * @return 宿舍修改视图
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap){
		Dormitory entity = 
			dormitoryService.selectDormById(id);
		mmap.put("dormitory", entity);
	    return getResponseViewPath("/edit");
	}
	
	/**
	 * <p>修改保存宿舍<p/>
	 * <p>访问权限: module-student:dormitory:edit</p>
	 * 
	 * @param entity 数据实体
	 * @param studentId 学生id
	 * @return http相应信息
	 */
	@ResponseBody
	@PostMapping("/edit")
	@RequiresPermissions("module-student:dormitory:edit")
	@Log(title = "宿舍列表", action = BusinessType.UPDATE)
	public AjaxResult editSave(Dormitory entity,String[] studentId){
		Integer result = 
			dormitoryService.updateDormById(entity,Arrays.asList(studentId));
		return toAjax(result);
	}
	
	/**
	 * <p>删除<p/>
	 * <p>访问权限: module-student:dormitory:remove</p>
	 * 
	 * @param ids 宿舍id
	 * @return http相应信息
	 */
	@ResponseBody
	@PostMapping( "/remove")
	@RequiresPermissions("module-student:dormitory:remove")
	@Log(title = "宿舍列表", action = BusinessType.DELETE)
	public AjaxResult remove(String[] ids){
		List<String> strs = Arrays.asList(ids);
		Integer result = dormitoryService.deleteDormByIds(strs);
		return toAjax(result);
	}
	
	/**
	 * <p>根据班级Id查询学生并且返回Select2格式<p/>
	 * 
	 * @param classId 班级id
	 * @return select2格式json数据
	 */
	@ResponseBody
	@GetMapping("/queryStudentFormatSelect2ByClassId")
	@RequiresPermissions(
		value = {"module-student:dormitory:add","module-student:dormitory:edit"},
		logical = Logical.OR
	)
	public AjaxResult queryStudentFormatSelect2ByClassId(String classId) {
		List<Select2> result = 
			studentService.selectStudentByCondition(
				Condition.create()
				.eq("now_class_id", classId)
				.eq("now_dormitory_id", 0)
			);
		return toSelect2Ajax(result);
	}
	
	/**
	 * <p>查询班级并以Select2的格式输出<p/>
	 * <p>
	 * 	访问权限: module-student:dormitory:add,module-student:dormitory:edit
	 * </p>
	 * 
	 * @return select2格式json数据
	 */
	@ResponseBody
	@GetMapping("/queryClassFormatSelect2")
	@RequiresPermissions(
		value = {"module-student:dormitory:add","module-student:dormitory:edit"},
		logical = Logical.OR
	)
	public AjaxResult queryClassFormatSelect2(){
		List<Select2> select2Result = classesService.selectListFmtSelect2();
		return toSelect2Ajax(select2Result);
	}
}
